import Cookie from "js-cookie"
// import router from "../../router"
export default {
    state: {
        isCollapse: false,
        // 面包屑列表
        tabsList: [
            {
                path: "/home",
                name: "home",
                label: "首页",
                icon: "s-home",
                url: "Home/Home",
            }
        ],
        // 当前点亮面包屑
        currentMenu: null,
        menu: []
    },
    mutations: {
        CollapseMenu(state) {
            state.isCollapse = !state.isCollapse
        },
        selectMenu(state, value) {
            // 判断点击的值是否为首页，不是就获取值跳转
            if (value.name !== "home") {
                state.currentMenu = value
                const result = state.tabsList.findIndex(item => item.name === value.name)
                if (result === -1) {
                    state.tabsList.push(value)
                }
            } else {
                state.currentMenu = null
            }
        },
        closeTag(state, value) {
            const result = state.tabsList.findIndex(item => item.name === value.name)
            state.tabsList.splice(result, 1)
        },
        setMenu(state, val) {
            state.menu = val
            Cookie.set("menu", JSON.stringify(val))
            console.log("setMenu on");

        },
        clearMenu(state) {
            state.menu = []
            Cookie.remove("menu")
            console.log("clearMenu on");
        },
        addMenu(state, router) {
            if (!Cookie.get("menu")) {
                return
            }
            const menu = JSON.parse(Cookie.get("menu"))
            state.menu = menu
            const menuArray = []
            menu.forEach(item => {
                if (item.children) {
                    item.children = item.children.map(item => {
                        item.component = () => import(`../../views/${item.url}`)
                        return item
                    })
                    menuArray.push(...item.children)
                } else {
                    item.component = () => import(`../../views/${item.url}`)
                    menuArray.push(item)
                }
            });
            console.log(menuArray);
            // 路由的动态添加
            menuArray.forEach(item => {
                router.addRoute("Main", item)
            });
            console.log(router);
            console.log("addMenu on");
        }
    }
}